<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/10/26
 * Time: 09:59
 */

namespace HiCommon\Model;

class StoreOrder extends Base
{

    protected $table = 'store_order';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];

    /**
     *  关联 活动信息
     **/
    public function activity()
    {
        return $this->belongsTo(Activity::class, 'activity_id');
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id');
    }

    public function shop_name()
    {
        return $this->belongsTo(Shop::class, 'shop_id')
            ->select(['id', 'name', 'phone', 'logo']);
    }

    /**
     *  关联 订单商品关系表
     **/
    public function getOrderGoods()
    {
        return $this->hasMany(StoreOrderGoods::class, 'store_order_id', 'id')
            ->with(['goods_refund_status', 'goods_product' => function ($query) {
                $query->with(['specs', 'goods_info']);
            }])
            ->select('id', 'store_order_id', 'goods_product_id', 'goods_id', 'goods_num', 'goods_price', 'goods_amount'
                , 'refund_price', 'is_refund', 'created_at', 'goods_name', 'goods_specs', 'goods_pic');
    }

    /**
     *  关联 订单商品关系表
     **/
    public function getOrderGoods2()
    {
        return $this->hasMany(StoreOrderGoods::class, 'store_order_id', 'id')->select('store_order_id', 'goods_id', 'is_refund','goods_name','goods_pic');
    }


    /**
     *  关联用户表
     *
     **/
    public function getUser()
    {
        return $this->belongsTo(Users::class, 'user_id', 'id')->select(['id', 'nickname', 'realname', 'email', 'phone', 'sex', 'head_img']);
    }

    /**
     *  关联用户表
     *
     **/
    public function user()
    {
        return $this->belongsTo(Users::class, 'user_id', 'id')->select(['id', 'nickname', 'realname', 'email', 'phone', 'sex', 'head_img']);
    }


    /**
     *  关联收货地址
     *
     **/
    public function getAddress()
    {
        return $this->belongsTo(Address::class, 'user_address_id', 'id')->select(['phone', 'province', 'city', 'area', 'address', 'id', 'username']);
    }

    /**
     *  关联快递公司
     *
     **/
    public function getExpress()
    {
        return $this->hasOne(Express::class, 'id', 'xpress_id')->select(['id', 'name', 'type']);
    }

    /**
     *  退货订单表
     **/
    public function order_refund()
    {
        return $this->hasMany(StoreOrderRefund::class, 'store_order_id', 'id')
            ->select(['id', 'store_order_id', 'apply_type', 'apply_state', 'refund_amount', 'created_at']);
    }

    /**
     *  取消订单原因
     *
     **/
    public function get_cancel_reason()
    {
        return $this->hasOne(CancelReason::class, 'id', 'cancel_reason_id')
            ->select(['id', 'reason', 'type']);
    }

    /**
     *  关联 订单商品关系表
     **/
    public function get_refund_price()
    {
        return $this->hasMany(StoreOrderGoods::class, 'store_order_id', 'id')
            ->select(['id', 'store_order_id', 'refund_price', 'is_refund', 'goods_num', 'goods_product_id']);
    }

    /**
     *  关联 订单评论
     **/
    public function goods_comment()
    {
        return $this->hasMany(UsersApprise::class, 'order_id', 'id')
            ->select(['id', 'order_id', 'goods_id', 'score', 'content', 'images', 'created_at']);
    }


    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getImgUrlAttribute($value)
    {
        $arr_data = [
            'src' => $value,
            'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        $s_created_at = $this->created_at;
        $arr_data = [
            'created_at' => ymd_friendly_date($s_created_at),
        ];

        return $arr_data;
    }


}